package com.quanshu.exchange.ui.gui.login;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.entity.StateNationalEntity;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseDialog;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

public class DialogFlagList extends BaseDialog {

    @BindView(R.id.content_recycler_view)
    RecyclerView recyclerView;
    private SingleAdapter<StateNationalEntity> adapter;

    public DialogFlagList(@NonNull Context context) {
        super(context);
    }

    public BehaviorSubject<StateNationalEntity> getItemClickSubject() {
        return itemClickSubject;
    }

    private BehaviorSubject itemClickSubject = BehaviorSubject.create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_recycler_layout);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public void init(@NonNull Context context) {
        Window window = getWindow();
        window.setWindowAnimations(R.style.BottomAnim);
        window.setGravity(Gravity.CENTER);
        //默认的Dialog只有5/6左右的宽度，改为全屏宽度,由dialog的布局自己来决定实际显示宽度
        window.getDecorView().setPadding(40, 0, 40, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SingleAdapter<>(new DataDisplayer<StateNationalEntity>(getContext(), R.layout.item_flag_dialog) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<StateNationalEntity> wrapper,
                                 final StateNationalEntity item,
                                 int position) {
                holder.setText(R.id.item_state_name, item.getFlag_name());
                holder.setImage(R.id.item_national_flag, item.getFlag_national());
                RxViewHelper.getInstance().clicks(holder.getView(R.id.item_rel), aVoid -> itemClickSubject.onNext(item));
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void setData(List<StateNationalEntity> dataList) {
        if (dataList != null && dataList.size() > 0) {
            adapter.setData(dataList);
        }
    }

    @Override
    public boolean isCancelAble() {
        return true;
    }
}
